package com.wisdomparking.control.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wisdomparking.bean.Manager;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.ManagerService;
import com.wisdomparking.tools.MD5;
import com.wisdomparking.tools.PageForAjax;
import com.wisdomparking.tools.ToolValid;

/**
 * 后台管理 - 管理员中心
 */
@RestController
@RequestMapping("/web/managerCenter")
public class ManagerController extends BaseController{
	
	@Autowired ManagerService managerService;
	
	/**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
    	
        return new ModelAndView("manager/index");
    }
	
	/**
     * 左侧
     *
     * @return
     */
    @RequestMapping(value = "/left")
    public ModelAndView left(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("manager/left");
    }
    
    /**
     * 转到管理员信息处理
     *
     * @return
     */
    @RequestMapping(value = "/handlePage")
    public ModelAndView handlePage() {
    	
    	if(ToolValid.isNotEmpty(request.getParameter("id"))){
    		Manager manager = managerService.getManager(Integer.parseInt(request.getParameter("id")));
    		
    		request.setAttribute("manager", manager);
    	}
    	
        return new ModelAndView("manager/op");
    }
	
    
    /**
	 * 获取管理员信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getManager")
	public ModelAndView getManager(Integer id){
		Manager manager = managerService.getManager(id);
		request.setAttribute("manager", manager);
		
		return new ModelAndView("manager/op");
	}
    
	
	/**
	 * 添加管理员
	 * @param manager
	 * @return
	 */
	@RequestMapping("/handleManager")
	public TempResult handleManager(Manager manager){
		boolean flag = false;
		
		if(ToolValid.isNotNull(manager)){
			Manager loginManger = (Manager)request.getSession().getAttribute(Constants.LOGININFO);
			if(ToolValid.isNotNull(loginManger)){
				manager.setPassword(MD5.getMD5Str(manager.getPassword()));
				manager.setManagerId(loginManger.getId());
				manager.setManagerGrade(2);
				if(ToolValid.isEmpty(manager.getId())){
					manager.setCreateTime(new Date());
					flag = managerService.addManager(manager, true);
				}
				else{
					flag = managerService.updateManager(manager, true);
				}
			}
		}
		
		return new TempResult(flag, "");
	}
	
	
	/**
	 * 删除管理员
	 * @param manager
	 * @return
	 */
	@RequestMapping("/delManager")
	public TempResult delManager(Integer id){
		boolean flag = false;
		Manager manager = managerService.getManager(id);
		manager.setDelFlag(true);
		flag = managerService.updateManager(manager, false);
		
		return new TempResult(flag, "");
	}
	
	
	
	/**
	 * 获取管理员列表
	 * @return
	 */
	@RequestMapping("/getManagerList")
	public ModelAndView getManagerList(){
		
		String url = request.getContextPath() + "/web/managerCenter/getManagerList";
		String query = "";
		PageForAjax pagecontent = null;
		
		Manager loginManger = (Manager)request.getSession().getAttribute(Constants.LOGININFO);
		
		int pageNo = 1;
		int pageSize = 10;
		int totalcount = 0;
		
		if(null!=request.getParameter("pageNo")){
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		
		if(null!=request.getParameter("pageSize")){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("start", (pageNo-1)*pageSize);
		map.put("limit", pageSize);
		map.put("delFlag", 0);
		
		if(ToolValid.isNotNull(loginManger)){
			switch (loginManger.getManagerGrade()) {
			case 0:
				break;
			case 1:
				map.put("managerId", loginManger.getId());
				break;
			case 2:
				map.put("managerId", loginManger.getManagerId());
				break;

			default:
				break;
			}
		}
		
		if(ToolValid.isNotEmpty(request.getParameter("key"))){
			
			map.put("userName", request.getParameter("key"));
			
			request.setAttribute("key", request.getParameter("key"));
		}
		
		List<Manager> list = managerService.getManagerList(map);
		totalcount = new Long(managerService.getCountByManager(map)).intValue();

		pagecontent = new PageForAjax(pageNo, pageSize, totalcount, url, query);

		request.setAttribute("managerList", list);
		request.setAttribute("nowpage", pagecontent);
		
		List<Manager> mList = managerService.getManagerList(null);
		Map<Integer, Manager> mMap = mList.stream().collect(Collectors.toMap(Manager::getId, (p)->p));
		request.setAttribute("managetMap", mMap);
		
		return new ModelAndView("manager/managers");
	}
	
}
